#!/usr/bin/env python
# encoding: utf-8


from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

from RayGraphWidget import RayCanvasWidget

import binascii
from Help import Help
from Help import WATER_CMD
help = Help()
from ui.motorControl import Ui_Dialog
class MotorControlWidget(QDialog, Ui_Dialog):
    signal_movePos = pyqtSignal(float,float,float)
    signal_setLimit = pyqtSignal(float,float,float,float,float,float)
    def __init__(self,parent=None,serial=''):
        super(MotorControlWidget, self).__init__(parent)
        self.setupUi(self)
        self.doubleSpinBox_x.setRange(-300,300)
        self.doubleSpinBox_y.setRange(-300,300)
        self.doubleSpinBox_z.setRange(-400,400)
        self.pushButton_movePos.clicked.connect(self.movePos)
        self.pushButton_stop.clicked.connect(self.moveStop)
        self.pushButton_readLimit.clicked.connect(self.readLimit)
        self.pushButton_setLimit.clicked.connect(self.setLimit)
        self.serial = serial

        self.limit_x1 = 0
        self.limit_x2 = 0
        self.limit_y1 = 0
        self.limit_y2 = 0
        self.limit_z1 = 0
        self.limit_z2 = 0

    def movePos(self):
        x = self.doubleSpinBox_x.value()
        y = self.doubleSpinBox_y.value()
        z = self.doubleSpinBox_z.value()
        print("移动到坐标：(%s,%s,%s)"%(x,y,z))
        self.signal_movePos.emit(x,y,z)

    def updatePos(self, x, y, z):
        self.label_x.setText('X:%3.3f'%float(x))
        self.label_y.setText('Y:%3.3f'%float(y))
        self.label_z.setText('Z:%3.3f'%float(z))
        self.updateLimit(
            self.limit_x1 - float(x),
            self.limit_x2 - float(x),
            self.limit_y1 - float(y),
            self.limit_y2 - float(y),
            self.limit_z1 - float(z),
            self.limit_z2 - float(z),
        )

    def updateLimit(self,x1,x2,y1,y2,z1,z2):
        self.lineEdit_limit_x1.setText(str(x1))
        self.lineEdit_limit_x2.setText(str(x2))
        self.lineEdit_limit_y1.setText(str(y1))
        self.lineEdit_limit_y2.setText(str(y2))
        self.lineEdit_limit_z1.setText(str(z1))
        self.lineEdit_limit_z2.setText(str(z2))

    def readLimit(self):
        """读取限位"""
        cmd = WATER_CMD['读取限位']
        self.cmd_send(cmd)

    def setLimit(self):
        self.limit_x1 = float(self.lineEdit_limit_x1.text())
        self.limit_x2 = float(self.lineEdit_limit_x2.text())
        self.limit_y1 = float(self.lineEdit_limit_y1.text())
        self.limit_y2 = float(self.lineEdit_limit_y2.text())
        self.limit_z1 = float(self.lineEdit_limit_z1.text())
        self.limit_z2 = float(self.lineEdit_limit_z2.text())
        self.signal_setLimit.emit(
            float(self.lineEdit_limit_x1.text()),
            float(self.lineEdit_limit_x2.text()),
            float(self.lineEdit_limit_y1.text()),
            float(self.lineEdit_limit_y2.text()),
            float(self.lineEdit_limit_z1.text()),
            float(self.lineEdit_limit_z2.text())
        )

    def moveStop(self):
        """移动停止"""
        cmd = WATER_CMD['停止']
        self.cmd_send(cmd)

    def cmd_send(self, cmd_list):
        cmd = help.cmd_build(cmd_list)
        if self.serial.isOpen():
            data = binascii.unhexlify(cmd)
            print('发送命令：', ' '.join([cmd[i * 2:i * 2 + 2] for i in range(0, int(len(cmd) / 2))]))
            self.serial.write(data)
        else:
            QMessageBox.information(self, '通信提示', '通信异常,请检查通信接口', QMessageBox.Yes)

from ui.moveShow import Ui_DockWidget
class MoveShowDock(QDockWidget, Ui_DockWidget):
    signal_movePos = pyqtSignal(float,float,float)
    def __init__(self,parent=None):
        super(MoveShowDock, self).__init__(parent)
        self.setupUi(self)
        self.rayGL = RayCanvasWidget()
        self.verticalLayout.addWidget(self.rayGL)

    def updatePos(self, x, y, z):
        self.label_x.setText('X:%3.3f'%float(x))
        self.label_y.setText('Y:%3.3f'%float(y))
        self.label_z.setText('Z:%3.3f'%float(z))
        self.rayGL.updatePos(x,y,z)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    # win = MotorControlDock()
    win = MotorControlWidget()
    win.show()
    app.exec_()

